﻿// CCheckClientDlg.cpp: 实现文件
//

#include "pch.h"
#include "HotelManagementMFC.h"
#include "afxdialogex.h"
#include "CCheckClientDlg.h"


// CCheckClientDlg 对话框

IMPLEMENT_DYNAMIC(CCheckClientDlg, CDialog)

CCheckClientDlg::CCheckClientDlg(CWnd* pParent /*=nullptr*/)
	: CDialog(IDD_DLG_CLIENT_CHECK, pParent)
{
	saleSTC_Ptr = nullptr;
}

CCheckClientDlg::~CCheckClientDlg()
{
}

void CCheckClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_SEARCH_PAPERNUM, m_sPaperNum);
	DDX_Control(pDX, IDC_NUM, m_nNum);
	DDX_Control(pDX, IDC_NAME, m_nName);
	DDX_Control(pDX, IDC_SEX, m_nSex);
	DDX_Control(pDX, IDC_ROOMNUM, m_nRoomNum);
	DDX_Control(pDX, IDC_TIME_IN, m_nTimeIn);
	//  DDX_Control(pDX, IDC_TIME_OUT, m_nTimeOut);
	DDX_Control(pDX, IDC_PREPAYMENT, m_nPrepayment);
	DDX_Control(pDX, IDC_REAL, m_nRealPayment);
	DDX_Control(pDX, IDC_SALE, m_combSales);
	DDX_Control(pDX, IDC_DESCRIPTION, m_nDescription);
	DDX_Control(pDX, IDC_CHARGED, m_nChargedPayment);
	DDX_Control(pDX, IDOK, m_bCheck);
	DDX_Control(pDX, IDC_TIME_OUT, m_tOutTime);
	DDX_Control(pDX, IDC_FROM, m_nFrom);
}


BEGIN_MESSAGE_MAP(CCheckClientDlg, CDialog)
	ON_BN_CLICKED(ID_SEARCH, &CCheckClientDlg::OnBnClickedSearch)
	ON_CBN_SELCHANGE(IDC_SALE, &CCheckClientDlg::OnSelchangeSale)
	ON_BN_CLICKED(IDOK, &CCheckClientDlg::OnSaveClient)
END_MESSAGE_MAP()


BOOL CCheckClientDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_combSales.SetWindowTextW(L"- 请选择 -");

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}


// CCheckClientDlg 消息处理程序


void CCheckClientDlg::OnBnClickedSearch()
{
	// TODO: 在此添加控件通知处理程序代码
	// 清空comb
	m_combSales.ResetContent();
	m_combSales.SetWindowTextW(L"- 请选择 -");

	CString strPaperNum;
	m_sPaperNum.GetWindowText(strPaperNum);
	if (strPaperNum.IsEmpty())
	{
		AfxMessageBox(L"证件号码不能为空，请重新输入！", MB_OK);
		return;
	}

	CString strSQL;
	strSQL.Format(L"Select ClientNo, ClientName, Sex, NativePlace, PaperNum, InDate, RoomNo, Deposit From Client Where PaperNum = '%s'", strPaperNum);

	try {
		m_pRecordSet.CreateInstance(__uuidof(Recordset));
		m_pRecordSet->Open(strSQL.AllocSysString(),
			theApp.m_pConnection.GetInterfacePtr(),
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);

		// 使确定按钮可用
		m_bCheck.EnableWindow(1);
		// 遍历结果集
		CString tmp;
		_variant_t Holder;
		// 判断为空
		if (!m_pRecordSet->GetRecordCount() == 0)
		{
			theApp.m_GetCollect(m_pRecordSet, Holder, tmp, "ClientNo");
			m_nNum.SetWindowTextW(tmp);

			theApp.m_GetCollect(m_pRecordSet, Holder, tmp, "ClientName");
			m_nName.SetWindowTextW(tmp);

			theApp.m_GetCollect(m_pRecordSet, Holder, tmp, "Sex");
			m_nSex.SetWindowTextW(tmp);

			theApp.m_GetCollect(m_pRecordSet, Holder, tmp, "NativePlace");
			m_nFrom.SetWindowTextW(tmp);

			theApp.m_GetCollect(m_pRecordSet, Holder, tmp, "InDate");
			m_nTimeIn.SetWindowTextW(tmp);

			theApp.m_GetCollect(m_pRecordSet, Holder, tmp, "RoomNo");
			m_nRoomNum.SetWindowTextW(tmp);

			theApp.m_GetCollect(m_pRecordSet, Holder, tmp, "Deposit");
			m_nPrepayment.SetWindowTextW(tmp);
		}
		else
		{
			AfxMessageBox(L"没有客户数据，请重新输入！", MB_OK);
			return;
		}
	}
	catch (_com_error& e)
	{
		AfxMessageBox(e.Description());
	}


	// 计算总入住时间
	// 结算日期
	CTime checkTime;
	m_tOutTime.GetTime(checkTime);

	// 入住时间
	CString strInTime;
	m_nTimeIn.GetWindowTextW(strInTime);
	CStringArray resArr;
	CString split_flag("-");

	// 获取间隔时间
	theApp.SplitCString(strInTime, split_flag, resArr);
	int Year = _ttoi(resArr.GetAt(0));
	int Month = _ttoi(resArr.GetAt(1));
	int Day = _ttoi(resArr.GetAt(2));
	CTime inTime(Year, Month, Day, 0, 0, 0);

	CTimeSpan span = checkTime - inTime;
	int day_span = span.GetDays();
	if (span.GetHours() >= 12)
		day_span++;

	// 查找房间价格
	double Price = 0;
	CString strRoomNo;
	m_nRoomNum.GetWindowTextW(strRoomNo);

	// 应付金额
	CString strPrice;
	double ChargedPayPrice;

	_RecordsetPtr pRs;
	_variant_t Holder;
	CString strSubSQL;
	strSubSQL.Format(L"Select Price From Room, RoomType Where Room.RoomType = RoomType.RoomTypeNo And RoomCode = '%s'", strRoomNo);
	try
	{
		pRs.CreateInstance(__uuidof(Recordset));
		pRs->Open(strSubSQL.AllocSysString(),
			theApp.m_pConnection.GetInterfacePtr(),
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
		if (pRs->RecordCount != 0)
		{
			Holder = pRs->GetCollect("Price");
			if (Holder.vt != VT_NULL)
				Price = _ttoi((_bstr_t)Holder);
		}
		else
			AfxMessageBox(L"未查询到房间价格数据，请重试！", MB_OK);
		pRs->Close();

		// 计算应付金额
		ChargedPayPrice = Price * day_span;
		strPrice.Format(L"%0.2f", ChargedPayPrice);
		m_nChargedPayment.SetWindowTextW(strPrice);
	}
	catch (_com_error& e)
	{
		AfxMessageBox(e.Description());
	}

	// 查询优惠折数
	CString strSalesSQL(L"Select SalesCode, SalesName, SalesRate From Sales");
	try {
		m_pRecordSet.CreateInstance(__uuidof(Recordset));
		m_pRecordSet->Open(strSalesSQL.AllocSysString(),
			theApp.m_pConnection.GetInterfacePtr(),
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
		if (m_pRecordSet->RecordCount != 0)
		{
			int iAddStr;
			_variant_t Holder;

			m_pRecordSet->MoveFirst();
			while (!m_pRecordSet->adoEOF)
			{
				CString code, name, rate;
				theApp.m_GetCollect(m_pRecordSet, Holder, code, "SalesCode");
				theApp.m_GetCollect(m_pRecordSet, Holder, name, "SalesName");
				theApp.m_GetCollect(m_pRecordSet, Holder, rate, "SalesRate");

				iAddStr = m_combSales.AddString(name);
				// 堆内存指针
				saleSTC_Ptr = new SalesSTC();
				saleSTC_Ptr->code = _ttoi(code);
				saleSTC_Ptr->name = name;
				saleSTC_Ptr->rate = _ttof(rate);
				m_combSales.SetItemDataPtr(iAddStr, saleSTC_Ptr);
				m_pRecordSet->MoveNext();
			}
		}
		else
		{
			m_combSales.SetWindowTextW(L"当前没有优惠活动！");
		}
		// TODO: 
		CString strPrepayment;
		m_nPrepayment.GetWindowTextW(strPrepayment);
		double RealPrice = ChargedPayPrice - _ttof(strPrepayment);
		CString strRealPrice;
		strRealPrice.Format(L"%0.2f", RealPrice);
		m_nRealPayment.SetWindowTextW(strRealPrice);
	}
	catch (_com_error& e)
	{
		AfxMessageBox(e.Description());
	}
}


void CCheckClientDlg::OnSelchangeSale()
{
	// TODO: 在此添加控件通知处理程序代码
	int nIndex = m_combSales.GetCurSel();
	// 默认折数（散客）
	//CString rate = L"1.0";
	//CString* rate_ptr;
	//DWORD_PTR rate;
	if (nIndex == CB_ERR)
		AfxMessageBox(L"没有选择折扣类型！", MB_OK);
	else
	{
		saleSTC_Ptr = (SalesSTC*)m_combSales.GetItemDataPtr(nIndex);
	}

	CString total, pre, RealPrice;
	m_nChargedPayment.GetWindowTextW(total);
	m_nPrepayment.GetWindowTextW(pre);
	RealPrice.Format(L"%0.2f", _ttof(total) * saleSTC_Ptr->rate - _ttof(pre));
	m_nRealPayment.SetWindowTextW(RealPrice);
}


void CCheckClientDlg::OnSaveClient()
{
	// TODO: 在此添加控件通知处理程序代码

	CString strNo, strName, strSex, strFrom, strPaperNum, strInDate, strRoomNo, strDeposit, strCheckDate, strTotalMoney, strRepay, strFactPay, strRemark;

	double numDeposit, numTotalMoney, numRepay, numFactMoney;
	int numSaleNo;

	m_nNum.GetWindowTextW(strNo);
	m_nName.GetWindowTextW(strName);
	m_nSex.GetWindowTextW(strSex);
	m_nFrom.GetWindowTextW(strFrom);
	m_sPaperNum.GetWindowTextW(strPaperNum);
	m_nTimeIn.GetWindowTextW(strInDate);
	m_nRoomNum.GetWindowTextW(strRoomNo);

	// 预交
	m_nPrepayment.GetWindowTextW(strDeposit);
	numDeposit = _ttof(strDeposit);

	m_tOutTime.GetWindowTextW(strCheckDate);
	strCheckDate.Replace(L"/", L"-");

	// 应交
	m_nChargedPayment.GetWindowTextW(strTotalMoney);
	numTotalMoney = _ttof(strTotalMoney);

	// TODO: What Repay
	m_combSales.GetWindowTextW(strRepay);
	numRepay = _ttof(strRepay);

	// 实收
	m_nRealPayment.GetWindowTextW(strFactPay);
	numFactMoney = _ttof(strFactPay);

	saleSTC_Ptr = (SalesSTC*)m_combSales.GetItemDataPtr(m_combSales.GetCurSel());
	numSaleNo = saleSTC_Ptr->code;

	m_nDescription.GetWindowTextW(strRemark);


	CString strSQL, strSubSQL, strSubSQL_2;
	strSQL.Format(L"Delete From Client Where PaperNum = '%s'", strPaperNum);
	strSubSQL.Format(L"Update Room Set FactPeopleNum = 0 Where RoomCode = '%s'", strRoomNo);
	strSubSQL_2.Format(L"Insert Into HistoryClient(ClientNo, ClientName, Sex, NativePlace, PaperNum, InDate, RoomNo, Deposit, CheckDate, TotalMoney, Repay, FactMoney, Sales, Remark) Values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%f', '%s', '%f', '%f', '%f', '%d', '%s')",
		strNo, strName, strSex, strFrom, strPaperNum, strInDate, strRoomNo, numDeposit, strCheckDate, numTotalMoney, numRepay, numFactMoney, numSaleNo, strRemark);

	try {
		theApp.m_pConnection->Execute(strSQL.AllocSysString(), NULL, adCmdText);
		theApp.m_pConnection->Execute(strSubSQL.AllocSysString(), NULL, adCmdText);
		theApp.m_pConnection->Execute(strSubSQL_2.AllocSysString(), NULL, adCmdText);
		AfxMessageBox(L"用户结算成功！");
	}
	catch (_com_error& e)
	{
		AfxMessageBox(e.Description());
	}

	// 回收saleSTC_Ptr
	int cout = m_combSales.GetCount();
	if (cout != 0)
	{
		while (cout--)
		{
			saleSTC_Ptr = (SalesSTC*)m_combSales.GetItemDataPtr(cout);
			if (saleSTC_Ptr != nullptr)
				delete saleSTC_Ptr;
		}
	}
	saleSTC_Ptr = nullptr;
	CDialog::OnOK();
}
